********************************************************************************
** 	TITLE:		UK17_results                                                  ** 
**	AUTHOR:	    Philippe Mongrain                                             **
**	DATA:       uk17                                                          **
**	VERSION:	Stata 16					                                  **
**	DATE:		October 2022  				                                  **
********************************************************************************

* Version control

version 16.0

* Open log file

capture log close
log using "UK17_results.smcl", replace

* Import the dataset

copy https://researchbriefings.files.parliament.uk/documents/CBP-7979/HoC-GE2017-results-by-candidate.csv uk17.csv, replace
type uk17.csv
import delimited uk17.csv, clear

* Generate rank of parties

gsort constituency_name -votes
bysort constituency_name : gen rank = _n

gen first = party_name if rank == 1
gen second = party_name if rank == 2
gen third = party_name if rank == 3

bysort constituency_name : gen winner17 = first[1]
bysort constituency_name : gen runnerup17 = second[2]
bysort constituency_name : gen thirdplace17 = third[3]

* Generate margins of victory

bysort constituency_name : gen margin = (share[1] - share[2])*100

* Generate effective number of electoral parties

gen sqrvotepr = (share)^2

bysort constituency_name : egen sumsqrvotepr = sum(sqrvotepr)

gen enep = 1/sumsqrvotepr

* Labelling parties

replace party_name = "1" if party_name == "Conservative"
replace party_name = "2" if party_name == "Labour"
replace party_name = "3" if party_name == "Liberal Democrat"
replace party_name = "4" if party_name == "Scottish National Party"
replace party_name = "5" if party_name == "Plaid Cymru"
replace party_name = "6" if party_name == "UK Independence Party"
replace party_name = "7" if party_name == "Green"
replace party_name = "8" if party_name == "British National Party"
replace party_name = "9" if party_name!="1" & party_name!="2" & party_name!="3" & party_name!="4" & party_name!="5" & party_name!="6" & party_name!="7"

destring (party_name), replace

replace winner17 = "1" if winner17 == "Conservative"
replace winner17 = "2" if winner17 == "Labour"
replace winner17 = "3" if winner17 == "Liberal Democrat"
replace winner17 = "4" if winner17 == "Scottish National Party"
replace winner17 = "5" if winner17 == "Plaid Cymru"
replace winner17 = "6" if winner17 == "UK Independence Party"
replace winner17 = "7" if winner17 == "Green"
replace winner17 = "8" if winner17 == "British National Party"
replace winner17 = "9" if winner17!="1" & winner17!="2" & winner17!="3" & winner17!="4" & winner17!="5" & winner17!="6" & winner17!="7"

destring (winner17), replace

replace runnerup17 = "1" if runnerup17 == "Conservative"
replace runnerup17 = "2" if runnerup17 == "Labour"
replace runnerup17 = "3" if runnerup17 == "Liberal Democrat"
replace runnerup17 = "4" if runnerup17 == "Scottish National Party"
replace runnerup17 = "5" if runnerup17 == "Plaid Cymru"
replace runnerup17 = "6" if runnerup17 == "UK Independence Party"
replace runnerup17 = "7" if runnerup17 == "Green"
replace runnerup17 = "8" if runnerup17 == "British National Party"
replace runnerup17 = "9" if runnerup17!="1" & runnerup17!="2" & runnerup17!="3" & runnerup17!="4" & runnerup17!="5" & runnerup17!="6" & runnerup17!="7"

destring (runnerup17), replace

replace thirdplace17 = "1" if thirdplace17 == "Conservative"
replace thirdplace17 = "2" if thirdplace17 == "Labour"
replace thirdplace17 = "3" if thirdplace17 == "Liberal Democrat"
replace thirdplace17 = "4" if thirdplace17 == "Scottish National Party"
replace thirdplace17 = "5" if thirdplace17 == "Plaid Cymru"
replace thirdplace17 = "6" if thirdplace17 == "UK Independence Party"
replace thirdplace17 = "7" if thirdplace17 == "Green"
replace thirdplace17 = "8" if thirdplace17 == "British National Party"
replace thirdplace17 = "9" if thirdplace17!="1" & thirdplace17!="2" & thirdplace17!="3" & thirdplace17!="4" & thirdplace17!="5" & thirdplace17!="6" & thirdplace17!="7"

destring (thirdplace17), replace

label define party 1 "Conservative" 2 "Labour" 3 "Liberal Democrat" 4 "SNP" 5 "Plaid Cymru" 6 "UKIP" 7 "Green" 8 "BNP" 9 "Other"
label values party_name winner17 runnerup17 thirdplace17 party
 
* Selecting, ordering, and sorting variables

keep ons_id constituency_name party_name rank votes share margin enep winner17 runnerup17 thirdplace17

order ons_id constituency_name party_name rank votes share margin enep winner17 runnerup17 thirdplace17

sort ons_id rank

drop if rank > 3

* Drop duplicates

duplicates tag ons_id, gen(dup)
duplicates drop ons_id, force
drop dup

* Change constituency ID

replace ons_id = subinstr(ons_id,"E1400","1",.)
replace ons_id = subinstr(ons_id,"S1400","2",.)
replace ons_id = subinstr(ons_id,"W0700","3",.)
replace ons_id = subinstr(ons_id,"N0600","4",.)

* Create election variable

gen election17 = "2017 British general election"

* Saving file

save UK17_results, replace

* Merge with UK15_results.dta and generate reelection variable

merge 1:1 _n using UK15_results.dta

* Generate variable for incumbent party reelection

gen reelected17 = 1 if winner17 == winner15
replace reelected17 = 0 if reelected17!=1

* Saving file

save UK1517_results, replace

log close